import { setStore, getStore, clearStore } from '@/utils/store'
// 用户、店铺信息相关
const user = {
	state: {
		userInfo: getStore('userInfo') || {},		//用户信息
		identityType: getStore('identityType') || '',		//用户身份
		access_token: getStore('access_token') || '',	//token, dGVzdDp0ZXN0:登录默认token
		refresh_token: getStore('refresh_token') || '',		//刷新token
	},
	
	mutations: {
		// 设置用户信息
		SET_USERINFO (state, userInfo) {
			state.userInfo = userInfo
			setStore({
				name: 'userInfo', 
				content: userInfo
			})
		},
		// 设置token
		SET_ACCESSTOKEN (state, access_token) {
			state.access_token = access_token
			setStore({
				name: 'access_token', 
				content: access_token
			})
		},
		
		// 设置刷新token
		SET_REFRESH_TOKEN (state, refresh_token) {
			state.refresh_token = refresh_token
			setStore({
				name: 'refresh_token', 
				content: refresh_token
			})
		},
		
		// 设置用户位置信息
		SET_USER_ADDRESS (state, address) {
			state.address = address
			setStore({
				name: 'address', 
				content: address
			})
		},
	},
	
	actions: {
		// 获取用户信息
		getUserInfo ({commit}, params) {
			return new Promise((resolve, reject) => {
				getUserInfo().then(res => {
					if(res.data.loginType == '0' && res.data){
						setUpdateLogin().then(succ => {})
					}
					resolve(res.data)
					commit('SET_USERINFO', res.data)
					commit('SET_USER_IDENTITIFY', res.data.identityType)
				})
			})
		},
		
		// 手机验证码登录
		phoneLogin ({commit}, params) {
			return new Promise((resolve, reject) => {
				phoneLogin({
					grant_type: 'mobile',
					mobile: 'SMS@' + params.mobile,
					code: params.code
				}).then(res => {
					resolve(res)
					commit('SET_ACCESSTOKEN', res.access_token)
					commit('SET_REFRESH_TOKEN', res.refresh_token)
				}).catch(err => {
					reject(err)
				})
			})
		},
		
		// 账号密码登录
		passwordLogin ({commit}, params) {
			return new Promise((resolve,reject) => {
				passwordLogin({
					grant_type: 'password',
					username: params.username,
					password: params.password
				}).then(res => {
					resolve(res)
					commit('SET_ACCESSTOKEN', res.access_token)
					commit('SET_REFRESH_TOKEN', res.refresh_token)
				}).catch(err => {
					reject(err)
				})
			})
		},
		
		// 微信小程序授权登录
		wechatMiniLogin ({commit}, params) {
			return new Promise((resolve, reject) => {
				wechatLogin({
					grant_type: 'mobile',
					mobile: 'MINI@' + params.phone
				}).then(res => {
					resolve(res)
					commit('SET_ACCESSTOKEN', res.access_token)
					commit('SET_REFRESH_TOKEN', res.refresh_token)
				}).catch(err => {
					reject(err)
				})
			})
		},
		
		//手机号一键登录
		phonePeonyLogin ({commit}, params) {
			return new Promise((resolve, reject) => {
				phonePeonyLogin({
					grant_type: 'mobile',
					mobile: 'SMS@' + params.mobile
				}).then(res => {
					resolve(res)
					commit('SET_ACCESSTOKEN', res.access_token)
					commit('SET_REFRESH_TOKEN', res.refresh_token)
				}).catch(err => {
					reject(err)
				})
			})
		},
		
		//微信登录，获取登录状态
		getWachatState({commit} , params) {
			return new Promise((resolve, reject) => {
				wechatAppLogin({
					grant_type: 'mobile',
					mobile: 'WX@' + params.openid + '@' + params.unionid
				}).then(res => {
					resolve(res)
					commit('SET_ACCESSTOKEN', res.access_token)
					commit('SET_REFRESH_TOKEN', res.refresh_token)
				}).catch(err => {
					reject(err)
				})
			})
		},
		
		//微信登录,绑定手机号
		bindWxOneKey({commit} , params) {
			return new Promise((resolve, reject) => {
				bindWxOneKey({
					wxOpenid: params.wxOpenid,
					wxUnionId: params.wxUnionId,
					phone: params.phone,
					wxNickname: params.phone
				}).then(res => {
					resolve(res)
					commit('SET_ACCESSTOKEN', res.access_token)
					commit('SET_REFRESH_TOKEN', res.refresh_token)
				}).catch(err => {
					reject(err)
				})
			})
		},
		
		//微信登录,绑定用户名密码
		wxUserName({commit} , params) {
			return new Promise((resolve, reject) => {
				wxUserName({
					wxOpenid: params.wxOpenid,
					wxUnionId: params.wxUnionId,
					wxNickname: params.wxNickname,
					username: params.username,
					password: params.password
				}).then(res => {
					resolve(res)
					commit('SET_ACCESSTOKEN', res.access_token)
					commit('SET_REFRESH_TOKEN', res.refresh_token)
				}).catch(err => {
					reject(err)
				})
			})
		},
		
		//微信登录,绑定手机号验证码
		wxPhone({commit} , params) {
			return new Promise((resolve, reject) => {
				wxPhone({
					wxOpenid: params.wxOpenid,
					wxUnionId: params.wxUnionId,
					wxNickname: params.wxNickname,
					phone: params.phone,
					smsCode: params.smsCode
				}).then(res => {
					resolve(res)
					commit('SET_ACCESSTOKEN', res.access_token)
					commit('SET_REFRESH_TOKEN', res.refresh_token)
				}).catch(err => {
					reject(err)
				})
			})
		},
		
		// 绑定邀请关系
		bindParent ({commit, state}) {
			// 邀请人id从缓存取，
			let inviterId = state.oldInviterId
			if(!inviterId || !state.userInfo.id || inviterId == 'undefined'){return}
			
			// 绑定邀请关系的前提是已经登录并且当前用户id不等于邀请人id
			if(state.access_token && state.userInfo.id !== inviterId){
				// 如果上级存在，就不需要重新绑定
				if(!state.userInfo.parentUserId || state.userInfo.parentUserId === '0'){
					bindParent({
						state: 4,		//后台约定商品分享固定值
						afterParentUserId: inviterId
					}).then(res => {
						if(res.data) {
							uni.showToast({
								icon:'none',
								title: res.data
							})
						}
						commit('SET_OLDINVITERID', '')
						commit('SET_OLDINVITECODE', '')
					})
				}
			}
		},
		
		// 退出
		loginOut ({commit}) {
			return new Promise((resolve,reject) => {
				commit('SET_USERINFO', '')
				commit('SET_USER_IDENTITIFY', '')
				commit('SET_ACCESSTOKEN', '')
				resolve()
			})
		},
		
		// 获取用户位置
		getUserAddress ({commit}, e) {
			return new Promise((resolve, reject) => {
				// #ifdef MP
				getMpRegeo(e.longitude, e.latitude).then(res => {
					if(res.regeocode && !res.regeocode.addressComponent.city){
						return reject()
					}
					let addressInfo = {
						latitude: e.latitude,
						longitude: e.longitude,
						address: res.regeocode.addressComponent
					}
					commit('SET_USER_ADDRESS', addressInfo)
					resolve(addressInfo)
				})
				// #endif 
				// #ifdef APP-PLUS
				getAppLocation(e.longitude, e.latitude).then(res => {
					if(res.regeocode && !res.regeocode.addressComponent.city){
						return reject()
					}
					let addressInfo = {
						latitude: e.latitude,
						longitude: e.longitude,
						address: res.regeocode.addressComponent
					}
					commit('SET_USER_ADDRESS', addressInfo)
					resolve(addressInfo)
				})
				// #endif
			})
		},
		
		
		// 保存聊天未读信息数量
		setChatNumber ({commit}, params) {
			return new Promise((resolve, reject) => {
				commit('SET_CHATLISTNUMBER', params)
				resolve()
			})
		},
		
		//socket退出
		loginSocketOut ({commit}) {
			return new Promise((resolve,reject) => {
				commit('SET_CHATLISTNUMBER', 0)
				resolve()
			})
		},
		
	}
}

export default user